/**
 * @author casazaft
 */
Package("j2j.event").Class("EventTarget", {
    EventTarget: function(config){
    },
    Prototype: function(){
        var defaultListeners = {};
        var eRe = /^(\w)(\w*)$/;
        var eFn = function(m, $1, $2){
            return "on" + $1.toUpperCase() + $2;
        };
        var Query = Import("j2j.dom.Query");
        return {
            em: j2j.event.EventMgr,
            cssm: j2j.css.Css,
            geo: j2j.dom.Geometry,
            select: function(selector){
                return new Query(selector).exec();
            },
            addEventListeners: function(listeners){
                listeners = config.listeners || defaultListeners;
                for (var e in listeners) {
                    this["on" + e] = listeners[e];
                }
            },
            addEventListener: function(type, fn){
                if (fn) {
                    this[type.replace(eRe, eFn)] = fn;
                }
            },
            removeEventListener: function(type, fn){
                delete this[type.replace(eRe, eFn)];
            },
            dispatchEvent: function(e){
                var fn = this[e.type.replace(eRe, eFn)];
                if (fn) 
                    fn.call(this, e);
                else {
                    throw Error("EventTarget" + this.getClass().getName() + " didn`t define " + e.type + " handler fn");
                }
            },
            finalize: function(obj){
                this.Super.finalize(this);
            }
        }
    }()
});
